<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/9/25
 * Time: 15:04
 */

namespace app\commi\controller;


use think\Db;

class Stop extends Base
{
    /**
     * 结束整场比赛
     * @return \think\response\Json
     */
    public function overMatch(){
        $match = Db::table('up_match')
            ->where('match_committee_id', session('com_id'))
            ->where('match_member_id', session('cm_id'))
            ->field('match_id, match_real_end_status')
            ->find();
        if($match['match_real_end_status'] == 2){
            return json(['code' => 100, 'msg' => '赛事已经结束']);
        }
        Db::startTrans();
        try{
            // 修改赛事实际结束状态
            Db::table('up_match')->where('match_id', $match['match_id'])->update(['match_real_end_status' => 2, 'match_status' => 3]);
            // 把该场赛事的所有已上岸签到的队员的下水签到和上岸签到状态改成已下水签到，已上岸签到
            $list = Db::table('up_match_apply_info')
                ->where('ma_match_id', $match['match_id'])
                ->where('ma_role', 1)
                ->field('ma_sgin_date, ma_up_date, ma_up, ma_id')
                ->select();
            foreach($list as $v){
                // 下水签到和上岸签到是同一天，说明肯定已经上岸签到了
                if(strtotime($v['ma_sgin_date']) == strtotime($v['ma_up_date'])){
                    Db::table('up_match_apply_info')->where('ma_id', $v['ma_id'])
                        ->update([
                            'ma_sgin_status' => 2,
                            'ma_down' => 2,
                            'ma_up' => 2
                        ]);
                }
            }
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}